﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using WebApplication1.Classes;

namespace WebApplication1.Conexao
{
    public class CategoriaBanco
    {
        private ConexaoBD conexaoBanco;
        public CategoriaBanco()
        {
            conexaoBanco = new ConexaoBD();
        }

        public List<Categoria> ListarCategoria()
        {
            String consulta = "select * from Categories";
            SqlCommand command = new SqlCommand(consulta, conexaoBanco.RealizarConexaoBD());
            SqlDataReader reader = command.ExecuteReader();
            List<Categoria> categorias = new List<Categoria>();

            while (reader.Read())
            {
                Categoria cat = new Categoria();
                cat.IdCategoria = reader.GetInt32(0);
                cat.NomeCategoria = reader.GetString(1);
                categorias.Add(cat);
            }
            return categorias;
        }

        public bool Adicionar(Categoria cat)
        {
            string adicionar = "INSERT INTO Categories (CategoryName)" + "Values (@n)";
            SqlCommand command = new SqlCommand(adicionar, conexaoBanco.RealizarConexaoBD());
            command.Parameters.Add(new SqlParameter("n", cat.NomeCategoria));
            int linhas = command.ExecuteNonQuery();

            return command.ExecuteNonQuery() > 0;



        }
        public Categoria Consultar(int id)
        {
            string consulta = "Select * from Categories where CategoryID = @id";
            SqlCommand command = new SqlCommand(consulta, conexaoBanco.RealizarConexaoBD());
            command.Parameters.Add(new SqlParameter("id", id));
            SqlDataReader reader = command.ExecuteReader();

            reader.Read();
            Categoria cat = new Categoria();
            cat.IdCategoria = reader.GetInt32(0);
            cat.NomeCategoria = reader.GetString(1);
            return cat;

        }

        public int ContarCategorias()
        {
            return ListarCategoria().Count;
        }




    }

}

